//
//  AppDelegate+AppService.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/2.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "AppDelegate+AppService.h"
#import <UMSocialCore/UMSocialCore.h>
#import <AlipaySDK/AlipaySDK.h>
#import "BaseNavigationConler.h"
@implementation AppDelegate (AppService)

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    //6.3的新的API调用，是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
    if (!result) {
        // 其他如支付等SDK的回调
        if ([url.host isEqualToString:@"safepay"]) {
            //跳转支付宝钱包进行支付，处理支付结果
            [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
                [self ProcessingPaymentResultsWithResult:resultDic];
            }];
        }
    }
    return result;
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"支付宝返回参数reslut = %@",resultDic);
           [self ProcessingPaymentResultsWithResult:resultDic];
        }];
    }else {// 芝麻信用回调
        
//        NSString *urlstr = [url absoluteString];
//        NSArray *strarray = [urlstr componentsSeparatedByString:@"="];
//        NSString *param = [strarray objectAtIndex:1];
//        NSArray *paramSrr = [param componentsSeparatedByString:@"&"];
//        NSString *params =[paramSrr firstObject];
//        NSString *sign =[strarray lastObject];
//        NSMutableDictionary *paraDic = @{}.mutableCopy;
//        [paraDic setObject:params forKey:@"params"];
//        [paraDic setObject:sign forKey:@"sign"];
//        [self updateDataWithParamter:paraDic];
//        //将 paraDic 上传服务器
        
        UINavigationController *NAV = (UINavigationController *)(self.window.rootViewController);
        UIViewController *likeCon = [self getLikeViewController];
        if (likeCon) {
            [NAV popToViewController:likeCon animated:YES];
        }else {
            [NAV popViewControllerAnimated:YES];
        }
    }
    
    return YES;
}

// 支付成功后处理
- (void)ProcessingPaymentResultsWithResult:(NSDictionary *)resultDic {
    NSString *status = [resultDic objectForKey:@"resultStatus"];
    if ([status isEqualToString:@"9000"]) {
        [MBProgressHUD gk_toastShowMessage:@"支付成功"];
        BaseNavigationConler *nav = (BaseNavigationConler *)self.window.rootViewController;
        [nav popViewControllerAnimated:YES];
        [self postNotic];
    }else {
        NSLog(@"支付失败");
        [MBProgressHUD gk_toastShowMessage:resultDic[@"memo"]];
    }
}
- (void)postNotic {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"paySuccessful" object:nil];
}
// 认证成功后 上传后台需要存储支付宝返回的数据
- (void)updateDataWithParamter:(NSMutableDictionary *)dic {
    
//    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] ]];
//    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
//        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
//            [MBProgressHUD gk_toastShowMessage:@"上传成功"];
//        }else if ([responseObject[@"rmg"] isEqualToString:@"用户不存在"]) {
//            [MBProgressHUD gk_toastShowMessage:@"请先登录"];
//        }else{
//            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
//        }
//    } FailureBlock:^(id error) {
//        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
//    }];
}

- (UIViewController *)getLikeViewController {
    UINavigationController *NAV = (UINavigationController *)(self.window.rootViewController);
    NSArray *controllers = NAV.childViewControllers;
    for (UIViewController *con in controllers) {
        if ([con isKindOfClass:NSClassFromString(@"personalVC")]) {
            return con;
        }
    }
    return nil;
}
@end
